package com.huawei123.hasee.cniao5store.mvp.interactor;

import com.huawei123.hasee.cniao5store.api.CategoryApi;
import com.huawei123.hasee.cniao5store.api.IGetDataDelegate;
import com.huawei123.hasee.cniao5store.base.BaseActivity;
import com.huawei123.hasee.cniao5store.bean.CategoryBean;
import com.huawei123.hasee.cniao5store.utils.JsonParseUtils;
import com.zhxu.library.http.HttpManager;
import com.zhxu.library.listener.HttpOnNextListener;

import javax.inject.Inject;

/**
 * 项目名称：Cniao5Store
 * 类描述：
 * 创建人：luohao
 * 创建时间：2018/1/30 0:10
 * 修改人：luohao
 * 修改时间：2018/1/30 0:10
 * 修改备注：
 */

public class CategoryInteractor {
    private IGetDataDelegate<CategoryBean> delegate;

    @Inject
    public CategoryInteractor() {
    }

    public void loadCategoryData(BaseActivity activity, IGetDataDelegate<CategoryBean> dataDelegate){
        this.delegate = dataDelegate;
        CategoryApi categoryApi = new CategoryApi(listener, activity);
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(categoryApi);
    }

    HttpOnNextListener listener =new HttpOnNextListener<CategoryBean>(){

        @Override
        public void onNext(CategoryBean categoryBean) {
            delegate.getDataSuccess(categoryBean);
        }
        /**
         * 缓存方法
         * @param string
         */
        @Override
        public void onCacheNext(String string) {
            super.onCacheNext(string);
            CategoryBean categoryBean = JsonParseUtils.parseCategoryBean(string);
            delegate.getDataSuccess(categoryBean);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            delegate.getDataError(e.getMessage());
        }
    };
}
